package cn.js.sip.rat.system.controller;

import cn.js.sip.rat.system.dto.AjaxResult;
import cn.js.sip.rat.system.entity.SysRoleEntity;
import cn.js.sip.rat.system.entity.SysUserEntity;
import cn.js.sip.rat.system.page.TableDataInfo;
import cn.js.sip.rat.system.service.SysRoleService;
import cn.js.sip.rat.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @ClassName: cn.js.sip.rat.system.controller
 * @Description: TODO
 * @author: xuxiaoli
 * @Date: 2020-01-03   20:59
 */
@Api("用户管理")
@Controller
@RequestMapping("system/role")
public class SysRoleController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(SysRoleController.class);
    private String prefix = "system/role";
    @Resource(name = "sysRoleService")
    private SysRoleService sysRoleService;
    @Resource(name = "sysUserService")
    private SysUserService sysUserService;

    @ApiOperation("view")
    @RequiresPermissions("system:role:view")
    @GetMapping
    public String user() {
        return prefix + "/role";
    }

    @ApiOperation("list")
    @RequiresPermissions("system:user:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysRoleEntity entity) {
        return sysRoleService.query(entity);
    }

    /**
     * 分配用户
     */
    @RequiresPermissions("system:role:edit")
    @GetMapping("/authUser/{roleId}")
    public String authUser(@PathVariable("roleId") String roleId, ModelMap mmap) {
        mmap.put("role", sysRoleService.findById(roleId));
        return prefix + "/authUser";
    }

    /**
     * 查询已分配用户角色列表
     */
    @RequiresPermissions("system:role:list")
    @PostMapping("/authUser/allocatedList")
    @ResponseBody
    public TableDataInfo allocatedList(SysUserEntity user) {

        return sysUserService.query(user);
    }

    /**
     * 修改角色
     */
    @GetMapping("/edit/{roleId}")
    public String edit(@PathVariable("roleId") String roleId, ModelMap mmap) {
        mmap.put("role", sysRoleService.findById(roleId));
        return prefix + "/edit";
    }

    /**
     * 修改保存角色
     */
    @RequiresPermissions("system:role:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysRoleEntity role) {
       /* sysRoleService.checkRoleAllowed(role);
        if (UserConstants.ROLE_NAME_NOT_UNIQUE.equals(sysRoleService.checkRoleNameUnique(role))) {
            return error("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if (UserConstants.ROLE_KEY_NOT_UNIQUE.equals(sysRoleService.checkRoleKeyUnique(role))) {
            return error("修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
        role.setUpdateBy(ShiroUtils.getLoginName());
        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(roleService.updateRole(role));*/
       return AjaxResult.success();
    }
}
